﻿Public Class DRUGAGENTOFDRUGMAKERCLASS
    Dim connect As CONDB = CONDB.NewConnection()

    Private MKAGID As Integer = Nothing
    Private AGENTCODE As Integer = Nothing
    Private DRGMID As Integer = Nothing
    Private STATUS = Nothing
    Private selectMakeragent = "SELECT a.`mkagid` AS 'ID', a.`drgmid` AS 'รหัสผู้ผลิต', b.`makername` AS 'ผู้ผลิต', a.`agentcode` AS 'รหัสตัวแทน', c.`company_name` AS 'ตัวแทนจำหน่าย', a.`status` AS 'สถานะการเปิดใช้งาน' FROM makeragent a LEFT JOIN masdrgmaker b ON a.drgmid = b.drgmid LEFT JOIN agent c ON a.agentcode = c.agentcode"

    Property MKAGID_ As Integer
        Get
            Return MKAGID
        End Get
        Set(value As Integer)
            MKAGID = value
        End Set
    End Property

    Property AGENTCODE_ As Integer
        Get
            Return AGENTCODE
        End Get
        Set(value As Integer)
            AGENTCODE = value
        End Set
    End Property

    Property DRGMID_ As Integer
        Get
            Return DRGMID
        End Get
        Set(value As Integer)
            DRGMID = value
        End Set
    End Property

    Property STATUS_ As Boolean
        Get
            Return STATUS
        End Get
        Set(value As Boolean)
            If value = True Then
                STATUS = 1
            Else
                STATUS = 0
            End If
        End Set
    End Property

    Property selectMakeragent_ As String
        Get
            Return selectMakeragent
        End Get
        Set(value As String)

        End Set
    End Property

    Public Function insertMakeragent() As Boolean
        If connect.GetTable("SELECT mkagid FROM makeragent WHERE agentcode = '" & AGENTCODE & "' AND drgmid = '" & DRGMID & "'").Rows.Count > 0 Then
            MsgBox("ตัวแทนจำหน่ายนี้จับคู่กับผู้ผลิตยานี้อยู่แล้ว")
        Else
            Try
                connect.ExecuteNonQuery("INSERT INTO makeragent (agentcode, drgmid, status) VALUES ('" & AGENTCODE & "', '" & DRGMID & "', '" & STATUS & "')")

                connect.Dispose()
                Return True
            Catch ex As Exception
                connect.Dispose()
                MsgBox(vbCrLf & ex.Message)
                Return False
            End Try
        End If
    End Function

    Public Function editMakeragent() As Boolean
        Try
            connect.ExecuteNonQuery("UPDATE makeragent SET agentcode = '" & AGENTCODE & "', drgmid = '" & DRGMID & "', status = '" & STATUS & "' WHERE mkagid = '" & MKAGID & "'")

            connect.Dispose()
            Return True
        Catch ex As Exception
            connect.Dispose()
            MsgBox(vbCrLf & ex.Message)
            Return False
        End Try
        connect.Dispose()
    End Function
End Class
